import os
import pandas as pd
from openpyxl.drawing.image import Image

from util import output_path


def generate_report(img_path, name, days):
    df = pd.DataFrame(days)
    df.rename(columns={
        "date": "日期",
        "hmax": "历史最高",
        "hmin": "历史最低",
        "max": "最高气温",
        "min": "最低气温",
        "jq": "节气",
        "maxobs": "实况高温",
        "minobs": "实况低温",
        "nl": "农历",
        "nlyf": "农历月份",
        "rainobs": "降雨量",
        "w1": "变化",
        "wd1": "风向",
        "wk": "周",
        "yl": "节日",
        "cloud1": "云量1",
        "cloud2": "云量2",
    }, inplace=True)

    xls_path = os.path.join(output_path, f"report_{name}.xlsx")

    with pd.ExcelWriter(xls_path, engine='openpyxl') as writer:
        df.to_excel(writer, index=False, startrow=20)  # 数据从第21行开始，防止图片覆盖
        worksheet = writer.sheets['Sheet1']
        img = Image(img_path)
        img.width = 400   # 调整图片宽度（单位像素）
        img.height = 200  # 调整图片高度（单位像素）
        worksheet.add_image(img, 'A1')  # 插入到A1
    os.remove(img_path)